home *** CD-ROM | disk | FTP | other *** search
/ Aminet 40 / Aminet 40 (2000)(Schatztruhe)[!][Dec 2000].iso / Aminet / dev / lang / Python16_Src.lha / Python16_Source / Python / thread_lwp.h < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-03  |  4.1 KB  |  180 lines

  1. #include <stdlib.h>
  2. #include <lwp/lwp.h>
  3. #include <lwp/stackdep.h>
  4.  
  5. #define STACKSIZE    1000    /* stacksize for a thread */
  6. #define NSTACKS        2    /* # stacks to be put in cache initialy */
  7.  
  8. struct lock {
  9.     int lock_locked;
  10.     cv_t lock_condvar;
  11.     mon_t lock_monitor;
  12. };
  13.  
  14.  
  15. /*
  16.  * Initialization.
  17.  */
  18. static void PyThread__init_thread _P0()
  19. {
  20.     lwp_setstkcache(STACKSIZE, NSTACKS);
  21. }
  22.  
  23. /*
  24.  * Thread support.
  25.  */
  26.  
  27.  
  28. int PyThread_start_new_thread _P2(func, void (*func) _P((void *)), arg, void *arg)
  29. {
  30.     thread_t tid;
  31.     int success;
  32.     dprintf(("PyThread_start_new_thread called\n"));
  33.     if (!initialized)
  34.         PyThread_init_thread();
  35.     success = lwp_create(&tid, func, MINPRIO, 0, lwp_newstk(), 1, arg);
  36.     return success < 0 ? 0 : 1;
  37. }
  38.  
  39. long PyThread_get_thread_ident _P0()
  40. {
  41.     thread_t tid;
  42.     if (!initialized)
  43.         PyThread_init_thread();
  44.     if (lwp_self(&tid) < 0)
  45.         return -1;
  46.     return tid.thread_id;
  47. }
  48.  
  49. static void do_PyThread_exit_thread _P1(no_cleanup, int no_cleanup)
  50. {
  51.     dprintf(("PyThread_exit_thread called\n"));
  52.     if (!initialized)
  53.         if (no_cleanup)
  54.             _exit(0);
  55.         else
  56.             exit(0);
  57.     lwp_destroy(SELF);
  58. }
  59.  
  60. void PyThread_exit_thread _P0()
  61. {
  62.     do_PyThread_exit_thread(0);
  63. }
  64.  
  65. void PyThread__exit_thread _P0()
  66. {
  67.     do_PyThread_exit_thread(1);
  68. }
  69.  
  70. #ifndef NO_EXIT_PROG
  71. static void do_PyThread_exit_prog _P2(status, int status, no_cleanup, int no_cleanup)
  72. {
  73.     dprintf(("PyThread_exit_prog(%d) called\n", status));
  74.     if (!initialized)
  75.         if (no_cleanup)
  76.             _exit(status);
  77.         else
  78.             exit(status);
  79.     pod_exit(status);
  80. }
  81.  
  82. void PyThread_exit_prog _P1(status, int status)
  83. {
  84.     do_PyThread_exit_prog(status, 0);
  85. }
  86.  
  87. void PyThread__exit_prog _P1(status, int status)
  88. {
  89.     do_PyThread_exit_prog(status, 1);
  90. }
  91. #endif /* NO_EXIT_PROG */
  92.  
  93. /*
  94.  * Lock support.
  95.  */
  96. PyThread_type_lock PyThread_allocate_lock _P0()
  97. {
  98.     struct lock *lock;
  99.     extern char *malloc();
  100.  
  101.     dprintf(("PyThread_allocate_lock called\n"));
  102.     if (!initialized)
  103.         PyThread_init_thread();
  104.  
  105.     lock = (struct lock *) malloc(sizeof(struct lock));
  106.     lock->lock_locked = 0;
  107.     (void) mon_create(&lock->lock_monitor);
  108.     (void) cv_create(&lock->lock_condvar, lock->lock_monitor);
  109.     dprintf(("PyThread_allocate_lock() -> %lx\n", (long)lock));
  110.     return (PyThread_type_lock) lock;
  111. }
  112.  
  113. void PyThread_free_lock _P1(lock, PyThread_type_lock lock)
  114. {
  115.     dprintf(("PyThread_free_lock(%lx) called\n", (long)lock));
  116.     mon_destroy(((struct lock *) lock)->lock_monitor);
  117.     free((char *) lock);
  118. }
  119.  
  120. int PyThread_acquire_lock _P2(lock, PyThread_type_lock lock, waitflag, int waitflag)
  121. {
  122.     int success;
  123.  
  124.     dprintf(("PyThread_acquire_lock(%lx, %d) called\n", (long)lock, waitflag));
  125.     success = 0;
  126.  
  127.     (void) mon_enter(((struct lock *) lock)->lock_monitor);
  128.     if (waitflag)
  129.         while (((struct lock *) lock)->lock_locked)
  130.             cv_wait(((struct lock *) lock)->lock_condvar);
  131.     if (!((struct lock *) lock)->lock_locked) {
  132.         success = 1;
  133.         ((struct lock *) lock)->lock_locked = 1;
  134.     }
  135.     cv_broadcast(((struct lock *) lock)->lock_condvar);
  136.     mon_exit(((struct lock *) lock)->lock_monitor);
  137.     dprintf(("PyThread_acquire_lock(%lx, %d) -> %d\n", (long)lock, waitflag, success));
  138.     return success;
  139. }
  140.  
  141. void PyThread_release_lock _P1(lock, PyThread_type_lock lock)
  142. {
  143.     dprintf(("PyThread_release_lock(%lx) called\n", (long)lock));
  144.     (void) mon_enter(((struct lock *) lock)->lock_monitor);
  145.     ((struct lock *) lock)->lock_locked = 0;
  146.     cv_broadcast(((struct lock *) lock)->lock_condvar);
  147.     mon_exit(((struct lock *) lock)->lock_monitor);
  148. }
  149.  
  150. /*
  151.  * Semaphore support.
  152.  */
  153. PyThread_type_sema PyThread_allocate_sema _P1(value, int value)
  154. {
  155.     PyThread_type_sema sema = 0;
  156.     dprintf(("PyThread_allocate_sema called\n"));
  157.     if (!initialized)
  158.         PyThread_init_thread();
  159.  
  160.     dprintf(("PyThread_allocate_sema() -> %lx\n", (long) sema));
  161.     return (PyThread_type_sema) sema;
  162. }
  163.  
  164. void PyThread_free_sema _P1(sema, PyThread_type_sema sema)
  165. {
  166.     dprintf(("PyThread_free_sema(%lx) called\n", (long) sema));
  167. }
  168.  
  169. int PyThread_down_sema _P2(sema, PyThread_type_sema sema, waitflag, int waitflag)
  170. {
  171.     dprintf(("PyThread_down_sema(%lx, %d) called\n", (long) sema, waitflag));
  172.     dprintf(("PyThread_down_sema(%lx) return\n", (long) sema));
  173.     return -1;
  174. }
  175.  
  176. void PyThread_up_sema _P1(sema, PyThread_type_sema sema)
  177. {
  178.     dprintf(("PyThread_up_sema(%lx)\n", (long) sema));
  179. }
  180.